package sdu.icat.ltp.api;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

/**
 * 队列
 * @author ljh_2015
 *
 */
public class ResultCache extends LinkedBlockingQueue<TransElement>{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final static Logger log = Logger.getLogger(ResultCache.class);
    private final static ResultCache _newInstance = new ResultCache();
    
    public static ResultCache newInstance() {
	return _newInstance;
    }
    
    /**
     * 插入元素
     * @param e 元素
     * @param params 绑定参数 <li>第一个参数：标题</li>
     * <li>第二个参数：段落</li>
     * <li>第三个参数：库名</li>
     */
    public void putElement(TransElement e,String...params) {
	try {
	    if(e==null) {
		return;
	    }
	    this.put(e);
	    log.debug("receive element");
	} catch (InterruptedException e1) {
	    log.error(e1.getMessage());
	}
    }
    
    /**
     * 拉取元素
     * <li>2s无数据，返回null</li>
     * @return
     */
    public TransElement pollElement() {
	try {
	    return this.poll(2, TimeUnit.SECONDS);
	} catch (InterruptedException e) {
	    log.error(e.getMessage());
	    return null;
	} finally {
	    log.debug("get element");
	}
    }
}
